package com.coolingme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.coolingme.constant.RedisConstant;
import com.coolingme.domain.entity.MenuBack;
import com.coolingme.domain.entity.Role;
import com.coolingme.mapper.MenuBackMapper;
import com.coolingme.mapper.PermissionsMapper;
import com.coolingme.mapper.RoleMapper;
import com.coolingme.server.RedisServer;
import com.coolingme.service.SecurityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author wangyue
 * @date 2020/7/28 14:46
 */
@Slf4j
@Service
public class SecurityServiceImpl implements SecurityService {

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Autowired
    private MenuBackMapper menuBackMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    RedisServer redisServer;

    @Override
    public Boolean resetSecurityInRedis() {
        // 0.清空redis
        log.info("==========清空权限key==========");
        Set<String> keys = redisServer.keys(RedisConstant.MENU_BACK_KEY_PREFIX + "*");
        keys.forEach(System.out::println);
        Long delNum = redisServer.del(keys);
        log.info("共删除" + delNum + "个权限key/n");

        log.info("==========清空页面key==========");
        Set<String> menuKeys = redisServer.keys(RedisConstant.ROLE_MENU__KEY_PREFIX + "*");
        menuKeys.forEach(System.out::println);
        Long delNum2 = redisServer.del(menuKeys);
        log.info("共删除" + delNum2 + "个页面key");

        // 1.查询所有角色
        List<Map<String, Object>> roleList = roleMapper
                .selectMaps(new LambdaQueryWrapper<Role>().select(Role::getRoleKey).eq(Role::isEnable, true));

        roleList.forEach(role -> {
            log.info("=======================");
            String roleKey = role.get("role_key").toString();
            log.info("roleKey:" + roleKey);

            // 2.查询角色所有菜单
            List<Map<String, Object>> menuList = menuBackMapper.listMenuByRoleKey(roleKey);

            menuList.forEach(menu -> {
                log.info("	menu:" + menu.get("id") + ":" + menu.get("url"));
                Integer menuId = ((Long) menu.get("id")).intValue();

                // 2.5.缓存角色对应的页面请求
                String menuKey = Role.getRedisMenuKey(roleKey);
                redisServer.sSet(menuKey, menu.get("url"));

                // 3.查询菜单所有按钮
                List<Integer> buttonIdList = menuBackMapper.listMenuButtonIdByRoleKeyAndMenuId(roleKey, menuId);
                buttonIdList.forEach(buttonId -> {
                    log.info("    	buttonId：" + buttonId);

                    // 4.查询按钮所有请求
                    Set<String> urls = permissionsMapper.listUrlByMenuButtonId(buttonId);
                    urls.forEach(url -> {
                        log.info("			url:" + url);
                    });

                    // 5.redis缓存
                    if (!urls.isEmpty()) {
                        String key = MenuBack.getRedisPermissionsKey(roleKey, menu.get("url").toString());
                        long result = redisServer.sSetBySet(key, urls);
                        log.info("	" + key + "成功放入" + result + "条路径");
                    }

                });
            });

        });
        return true;
    }
}
